package com.riven.bilibilidemo.fragment;

import android.graphics.Color;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;

import com.google.gson.Gson;
import com.riven.bilibilidemo.R;
import com.riven.bilibilidemo.adapter.MyRecyclerViewAdapter;
import com.riven.bilibilidemo.bean.BeanList;
import com.riven.bilibilidemo.bean.LiveAppIndexInfo;
import com.riven.bilibilidemo.interfaces.LiveServer;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observer;
import rx.Scheduler;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/*
*@date 创建时间 2017年3月10日07:46:39
*@作者：SunNaNa
*@作用：直播模块
*/

public class LiveFragment extends BaseFragment {
    private BeanList bean;
    private RecyclerView recyclerView;
    private SwipeRefreshLayout swipeRefreshLayout;

    @Override
    public int layoutID() {
        return R.layout.fragment_live;
    }
    @Override
    protected void initViews(View view) {
        recyclerView = (RecyclerView) view.findViewById(R.id.zhibo_recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        //刷新
        swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipeRefreshLayout);
        swipeRefreshLayout.setColorSchemeColors(Color.parseColor("#FF4081"));

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                    swipeRefreshLayout.setRefreshing(false);
                    }
                }, 300);
            }
        });
        //ok请求数据
        okRequestMessage();
    }

    @Override
    protected void initData() {

    }
    private void okRequestMessage() {
      Retrofit retrofit = new Retrofit.Builder()
              .baseUrl("http://live.bilibili.com/")
              .addConverterFactory(GsonConverterFactory.create())
              .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
              .build();
        LiveServer liveServer = retrofit.create(LiveServer.class);
        liveServer.getLiveAppIndex()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BeanList>() {

                    private BeanList.DatashouyeBean dataList;

                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(BeanList beanList) {
                        Log.d("zzz",beanList.toString());
                        dataList = beanList.getData();
                        recyclerView.setAdapter(new MyRecyclerViewAdapter(getActivity(),dataList));
                    }
                });

    }
}
